//
//  FFProgressHUD.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2018/1/4.
//  Copyright © 2018年 卢仕彤. All rights reserved.
//

#import "FFProgressHUD.h"
#import "NSString+Common.h"

//  提示类型
typedef NS_ENUM(NSInteger, LCProgressHUDType){
    FFProgressHUDTypeJHLoading,                 //菊花
    FFProgressHUDTypeOnlyText,                  //文字底部显示
    FFProgressHUDTypeOnlyTextCenter,            //文字中间显示
    FFProgressHUDTypeSuccess,                   //成功
    FFProgressHUDTypeError,                     //失败
    FFProgressHUDTypeCustomAnimation,           //自定义加载动画（序列帧实现）
};
@interface FFProgressHUD ()

@property (nonatomic,strong) MBProgressHUD  *HUD;

@end

@implementation FFProgressHUD

+ (instancetype)shareInstance{
    
    static FFProgressHUD *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[FFProgressHUD alloc] init];
    });
    return instance;
}


+ (void)show:(NSString *)msg inView:(UIView *)view type:(LCProgressHUDType) mytype{
    
    [self show:msg inView:view type:mytype customImgView:nil];
}


+ (void)show:(NSString *)msg inView:(UIView *)view type:(LCProgressHUDType)mytype customImgView:(UIImageView *)customImgView{
    
    if ([FFProgressHUD shareInstance].HUD != nil) {
        [[FFProgressHUD shareInstance].HUD hideAnimated:NO];
        [FFProgressHUD shareInstance].HUD = nil;
    }
    
    if ([UIScreen mainScreen].bounds.size.height == 480) {
        [view endEditing:YES];
    }
    
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    [FFProgressHUD shareInstance].HUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
    [FFProgressHUD shareInstance].HUD.backgroundColor = [UIColor clearColor];
    
    //  是否设置黑色背景
    [FFProgressHUD shareInstance].HUD.bezelView.backgroundColor = [UIColor blackColor];
    [FFProgressHUD shareInstance].HUD.contentColor = [UIColor whiteColor];
    
    [[FFProgressHUD shareInstance].HUD setRemoveFromSuperViewOnHide:YES];
    if(msg)[FFProgressHUD shareInstance].HUD.detailsLabel.text = msg;
    [FFProgressHUD shareInstance].HUD.detailsLabel.font = [UIFont systemFontOfSize:16];
    
    switch (mytype) {
            
        case FFProgressHUDTypeOnlyText:
            [FFProgressHUD shareInstance].HUD.mode = MBProgressHUDModeText;
            [[FFProgressHUD shareInstance].HUD setMargin:15];
            [FFProgressHUD shareInstance].HUD.offset = CGPointMake(0.f, MBProgressMaxOffset);
            break;
            
        case FFProgressHUDTypeJHLoading:
            [FFProgressHUD shareInstance].HUD.mode = MBProgressHUDModeIndeterminate;
            break;
            
        case FFProgressHUDTypeSuccess:
            [FFProgressHUD shareInstance].HUD.mode = MBProgressHUDModeCustomView;
            [FFProgressHUD shareInstance].HUD.customView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"success"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
            break;
            
        case FFProgressHUDTypeError:
            [FFProgressHUD shareInstance].HUD.mode = MBProgressHUDModeCustomView;
            [FFProgressHUD shareInstance].HUD.customView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"error"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
            break;
            
        case FFProgressHUDTypeOnlyTextCenter:
            [FFProgressHUD shareInstance].HUD.mode = MBProgressHUDModeCustomView;
            break;
            
        default:
            break;
    }
}




+ (void)show{
    [self show:nil inView:nil type:FFProgressHUDTypeJHLoading];
}


+ (void)showInView:(UIView *)view{
    [self show:nil inView:view type:FFProgressHUDTypeJHLoading];
}


+ (void)showProgress:(NSString *)msg{
    [self show:msg inView:nil type:FFProgressHUDTypeJHLoading];
}


+ (void)showMessage:(NSString *) msg{
    [self show:msg inView:nil type:FFProgressHUDTypeOnlyText];
    [[FFProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1];
}


+ (void)showMessageCenter:(NSString *) msg{
    [self show:msg inView:nil type:FFProgressHUDTypeOnlyTextCenter];
    [[FFProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1];
}


+ (void)showProgress:(NSString *)msg inView:(UIView *)view{
    [self show:msg inView:view type:FFProgressHUDTypeJHLoading];
}


+ (void)hide{
    if ([FFProgressHUD shareInstance].HUD != nil) {
        [[FFProgressHUD shareInstance].HUD hideAnimated:YES];
    }
}


+ (void)hideDelayTime:(NSInteger)delay{
    if ([FFProgressHUD shareInstance].HUD != nil) {
        [[FFProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:delay];
    }
}


+ (void)showMessage:(NSString *)msg inView:(UIView *)view{
    [self show:msg inView:view type:FFProgressHUDTypeOnlyText];
    [[FFProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1];
}


+ (void)showMessageCenter:(NSString *) msg inView:(UIView *)view{
    [self show:msg inView:view type:FFProgressHUDTypeOnlyTextCenter];
    [[FFProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1];
}

+ (void)showSuccess:(NSString *)msg{
    [self show:msg inView:nil type:FFProgressHUDTypeSuccess];
    [[FFProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1.0];
}


+ (void)showSuccess:(NSString *)msg inview:(UIView *)view{
    [self show:msg inView:view type:FFProgressHUDTypeSuccess];
    [[FFProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1.0];
}


+ (void)showError:(NSString *)msg{
    [self show:msg inView:nil type:FFProgressHUDTypeError];
    [[FFProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1.0];
}


+ (void)showError:(NSString *)msg inview:(UIView *)view{
    [self show:msg inView:view type:FFProgressHUDTypeError];
    [[FFProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1.0];
}

#pragma mark - 自定义HUD loading图片
+ (void)showGifToView:(UIView *)view afterDelay:(NSTimeInterval)delay
{
    [FFProgressHUD showGifToView:view afterDelay:delay withTitle:nil];
}
//+ (void)showGifToView:(UIView *)view  afterDelay:(NSTimeInterval)delay withTitle:(NSString *)title
//{
//    [FFProgressHUD shareInstance].HUD.detailsLabel.text = title;
//    //    [[self class]showGifToView:view afterDelay:delay];
//    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES ];
//    [FFProgressHUD shareInstance].HUD = hud;
//    //使用SDWebImage 放入gif 图片
//    UIImage *image = [UIImage sd_animatedGIFNamed:@"loading"];
//    //自定义imageView
//    UIImageView *cusImageV = [[UIImageView alloc] initWithImage:image];
//    //设置hud模式
//    hud.mode = MBProgressHUDModeCustomView;
//    //设置在hud影藏时将其从SuperView上移除,自定义情况下默认为NO
//    hud.removeFromSuperViewOnHide = YES;
//    //设置方框view为该模式后修改颜色才有效果
//    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
//    //设置方框view背景色
//    hud.bezelView.backgroundColor = [UIColor clearColor];
//    hud.bezelView.backgroundColor = [UIColor whiteColor];
//    //设置总背景view的背景色，并带有透明效果
//    hud.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0];
//    //hud.backgroundColor  = [UIColor clearColor];
//    hud.detailsLabel.text = title;
//    if ([NSString isBlankString:title]) {
//        hud.detailsLabel.text = @"正在努力加载...";
//    }
//    if (delay > 0) {
//        [[FFProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:delay];
//    }
//    hud.customView = cusImageV;
//}
@end

